﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using AlfarEngine.Utilities;

namespace AlfarEngine.Data
{
    public class ReferencedData<T>
    {
        protected int       mCounter;
        protected T         mData;
        protected string    mPath;

        public T data { get { return mData; } }
        public string path { get { return mPath; } }

        internal ReferencedData(string pPath)
        {
            mPath = pPath;

            try
            {
                mData = Engine.Content.Load<T>(pPath);
            }
            catch (Exception e)
            {
                Console.WriteLine("error loading data of type " + typeof(T).ToString() + " : " + e.Message);
            }
        }

        internal void IncrementCount()
        {
            mCounter++;
        }

        public void Unload()
        {
            mCounter--;
            if (mCounter <= 0)
            {
                LoadingDatabase<ReferencedData<T>, T>.Unload(mPath);
            }
        }
    }
}
